Migrating from Tronweb v5
Why v6.x
New Features & Capabilities
- TronWeb v6.x includes support for newer TRON protocol updates and TRON-based standards.
- Future feature development will only support version v6.x, while version v5.x will receive security fixes only.
Developer Experience Upgrades
- TronWeb v6.x provides improved TypeScript type definitions and compatibility, making it more developer-friendly and reducing bugs in large projects.
- Full TypeScript definitions for better IDE autocompletion and type safety.
- TronWeb v6.x uses Promises instead of callbacks, aligning with modern JavaScript practices like async/await.
Security Improvements
- Active versions like v6.x receive critical security patches.
- Regularly fixes vulnerable dependency packages to enhance security.
Better ESM support
- Native ESM module exports (import/export syntax) enable tree-shaking and seamless integration with modern bundlers like Webpack/Vite.
Better Documentation and Community Support
- As v6.x becomes the standard, more tutorials, community solutions, and official resources focus on it.
To better support its use in TypeScript project, we have rewritten the entire library in TypeScript. And to make TronWeb API more secure and more consistent, there are some breaking changes.
The breaking changes are listed below.
Change Default exports to Named exports
The default export of the project is no longer the TronWeb class. You can use import { TronWeb } from 'tronweb'; instead. And other constructors are also no longer mounted on the TronWeb class. You have to use the same way to import them. For example if you want to use TronWeb.utils, you should import { utils as TronWebUtils } from 'tronweb'; and then use TronWebUtils;
Example:
import { TronWeb, utils as TronWebUtils, Trx, TransactionBuilder, Contract, Event, Plugin } from 'tronweb';
Transaction and SignedTransaction
In typescript, those are two types. After you sign a transaction, the transaction object add signature prop automatically, and you can pass it to trx.sendRawTransaction as below:
await tronWeb.trx.sign(transaction, pk);
const result = {
transaction,
receipt: await tronWeb.trx.sendRawTransaction(transaction),
};
But if you do it in typescript, it will throw an error. Because transaction is Transaction type and trx.sendRawTransaction only accepts SignedTransaction type. To solve it, use the returned object from trx.sign like below:
const signedTransaction = await tronWeb.trx.sign(transaction, pk);
const result = {
transaction,
receipt: await tronWeb.trx.sendRawTransaction(signedTransaction),
};
Some methods are changed
TronWeb.createRandom
Change TronWeb.createRandom(options) to TronWeb.createRandom(password, path, wordlist).
TronWeb.fromMnemonic
Change TronWeb.fromMnemonic(mnemonic, path, wordlist) to TronWeb.fromMnemonic(mnemonic, path, password, wordlist).
utils.abi.decodeParams
The parameters are as follows:
utils.abi.decodeParams(names: string[], types: string[], output: string, ignoreMethodHash = false)
You must pass names argument. If there is no name, pass an empty array.